package com.lill.system.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lill.org.model.CompanyBranchModel;
import com.lill.org.model.CompanyInfoModel;
import com.lill.org.service.ICompanyBranchService;
import com.lill.org.service.ICompanyInfoService;
import com.lill.system.service.ISysUserService;

@Component
public class SysUserServe {
	
	@Autowired
    private ICompanyInfoService companyInfoService;
	@Autowired
	private ISysUserService sysUserService;
    @Autowired
    private ICompanyBranchService companyBranchService;

    public List<Map<String, Object>> queryUser(Map<String, Object> queryParam) {
        List<Map<String, Object>> mapList = sysUserService.queryUserLogin(queryParam);
        if (null != mapList && !mapList.isEmpty()) {
            List<Map<String, Object>> permBranchList = null;
            Map<String, Object> permBranch = null;
            for (int i = 0; i < mapList.size(); i++) {
                permBranchList = new ArrayList<Map<String, Object>>();
                CompanyInfoModel companyInfoVo = companyInfoService.queryObject(mapList.get(i).get("companyId").toString());
                if (2 == companyInfoVo.getType()) {
                    Map<String, Object> branchInfo = new HashMap<>();
                    branchInfo.put("branchId", mapList.get(i).get("companyId"));
                    branchInfo.put("branchName", mapList.get(i).get("companyName"));
                    branchInfo.put("branchCode", mapList.get(i).get("companyCode"));
                    mapList.get(i).putAll(branchInfo);
                } else {
                    Map<String, Object> branchInfo = companyBranchService.queryUserFirstBranch(mapList.get(i).get("departId").toString());
                    if (branchInfo != null) {
                        mapList.get(i).put("branchId",branchInfo.get("branch_id"));
                        mapList.get(i).put("branchCode",branchInfo.get("branch_code"));
                        mapList.get(i).put("branchName",branchInfo.get("branch_name"));

                        permBranch = new HashMap<String, Object>();
                        permBranch.put("id", branchInfo.get("branch_id"));
                        permBranch.put("name", branchInfo.get("branch_name"));
                        permBranchList.add(permBranch);
                    } else {
                        branchInfo = new HashMap<>();
                        branchInfo.put("branchId", null);
                        branchInfo.put("branchName", "集团领导");
                        branchInfo.put("branchCode", null);
                        mapList.get(i).putAll(branchInfo);
                    }
                }

                Map<String, Object> userBranchs = new HashMap<String, Object>();
                userBranchs.put("userId", mapList.get(i).get("id").toString());
                userBranchs.put("companyId", mapList.get(i).get("companyId").toString());
                List<CompanyBranchModel> branchList = companyBranchService.queryUserBranch(userBranchs);
                for (CompanyBranchModel companyBranchVo : branchList) {
                    permBranch = new HashMap<String, Object>();
                    permBranch.put("id", companyBranchVo.getId());
                    permBranch.put("name", companyBranchVo.getName());
                    permBranchList.add(permBranch);
                }
                mapList.get(i).put("permBranchList", permBranchList);
                //放入app设置
                Map<String, Object> map = companyInfoService.queryThemeColor(userBranchs);
                mapList.get(i).put("colorMap", (map==null?new HashMap<String, Object>():map));
            }
        }
        return mapList;
    }

    /**
     * 查询小程序设置
     * companyCode 公司编码
     * branchCode 门店编码
     * @param map
     * @return
     */
//    public Map<String, Object> queryXCXAppMap(Map<String, Object> map) {
//        String companyCode = map.get("companyCode") + "";
//        boolean flag = false;
//        PsAppletSettingEntity psAppletSettingEntity = psAppletSettingService.queryCompanyCode(map);
//        if (psAppletSettingEntity != null && psAppletSettingEntity.getAppOriginId() != null && !"".equals(psAppletSettingEntity.getAppOriginId())) {
//            flag = true;
//        }
//        if (flag) {
//            map.put("XCXAppId", psAppletSettingEntity.getAppId());
//            map.put("XCXAppSecret", psAppletSettingEntity.getAppSecret());
//            map.put("XCXName", psAppletSettingEntity.getAppName());
//            map.put("XCXWeb", psAppletSettingEntity.getAppUrl() != null ? psAppletSettingEntity.getAppUrl() : "www.baidu.com");
//            map.put("XCXIsShare", psAppletSettingEntity.getIsShare());
//            map.put("XCXCompanyCode", companyCode);
//            map.put("XCXAppOriginId", psAppletSettingEntity.getAppOriginId());
//        } else {
//            map.put("XCXAppId", QRCodeUtilAppId.XCX_APPID);
//            map.put("XCXAppSecret", QRCodeUtilAppId.XCX_APPSECRET);
//            map.put("XCXWeb", QRCodeUtilAppId.XCX_WEB);
//            map.put("XCXName", QRCodeUtilAppId.XCX_NAME);
//            map.put("XCXIsShare", QRCodeUtilAppId.XCX_IS_SHARE);
//            map.put("XCXCompanyCode", QRCodeUtilAppId.XCX_COMPANY_CODE);
//            map.put("XCXAppOriginId", QRCodeUtilAppId.XCX_APP_ORIGINID);
//        }
//        return map;
//    }
}
